<?php

/**
 * lab model
 * @package modules.dept_laboratory.includes
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabItemFactorModel {
	private $labResultId     = '';
	private $nameText      = '';
	private $result     = '';
	private $normalResultMale     = '';
	private $normalResultFemale       = '';
	private $enumDataType     = '';
	private $unit      = '';
	private $logger;

	function __construct() {
		$this->logger = LoggerManager::getLogger('module.dept_laboratory.includes.LabResult.LabItemFactorModel');

	}
	public function fillDataForDisplay($labResultId, $nameText, $result, $normalResultMale,
	$normalResultFemale, $enumDataType, $enumunit) {
		$this->labResultId = $labResultId;
		$this->nameText = $nameText;
		$this->result = trim($result);
		$this->normalResultMale = $normalResultMale;
		$this->normalResultFemale = $normalResultFemale;
		$this->enumDataType = $enumDataType;
		try {
			$this->unit = Utils::getEnumTextByKey(EnumerationConstant::UNIT, $enumunit);
		} catch (Exception $ex) {
				
				
		}
	}
	public function getLabResultId () {
		return $this->labResultId;
	}
	public function getNameText () {
		return $this->nameText;
	}
	public function getResult () {
		return $this->result;
	}
	/**
	 *
	 * Only check for datatype number.
	 * This function only false if inputed result is number
	 * datatype is number and result is out of range.
	 * @param $gender : male, female
	 * @return
	 * true if in range
	 * false if out of range
	 */
	public function isNormal($gender) {
		try {
			if ($this->result == null || trim($this->result) == '') {
				//not check
				return true;
			}
			 
			if ($this->enumDataType == Utils::getEnumKey(EnumerationConstant::LAB_DATATYPE,
			EnumerationConstant::LAB_DATATYPE_NUMBER)) {
				$limits = '';
				if ($gender == 'male') {
					$limits = split('-', $this->getNormalResultMale());
				} else {
					$limits = split('-', $this->getNormalResultFemale());
				}
				 
				if (count($limits) == 2) {
					$lower = trim($limits[0]);
					$upper = trim($limits[1]);
					//only do check if all factors art numeric
					if (!is_numeric($this->result)) {
						$this->logger->warn('the result [' . $this->result . '] is not numeric');
						return false;
					} else if (is_numeric($lower) && is_numeric($upper)) {
						if ($this->result < $lower || $this->result > $upper) {
							return false;
						}
					}  else {
						$this->logger->warn('not numeric ' . 'r ' . is_numeric($this->result) . ' l: ' . is_numeric($lower). ' u: ' . is_numeric($upper));
					}
				}
			}
		} catch (Exception $ex) {
			$this->logger->warn('validate result got issue. detail :' . $ex->getMessage());

		}
		return true;
	}
	public function getNormalResultMale () {
		return $this->normalResultMale;
	}
	public function getNormalResultFemale () {
		return $this->normalResultFemale;
	}
	public function getUnit () {
		return $this->unit;
	}

	public function saveResult($result) {
		try {
			if ($result != null) {
				$result = trim($result);
			}
			if ($this->enumDataType == Utils::getEnumKey(EnumerationConstant::LAB_DATATYPE,
			EnumerationConstant::LAB_DATATYPE_NUMBER)) {
				//normalize numeric data before save to database
				//use dot [.] for decimal point
				$result = str_replace(',', '.', $result);				 
			}
		} catch (Exception $ex) {
			$this->logger->warn('save result got issue. detail :' . $ex->getMessage());
		}
		$this->result = $result;
		$labResultObj = Celini::newOrdo('LabResult');
		$labResultObj->setup($this->labResultId);
		$labResultObj->setResult($result);
		$labResultObj->persist();
	}
}
?>